/**
 - suppression
 - resize
 - rotation
 - clonage
 - editer les textes au double click
 - ajouter la save 
 - possibilité de paramétrer le pas de la rotation

 * note
 * @author Thot
 */

function TransformableObject()
{
	tomahawk_ns.Sprite.apply(this);
	this.init(0,0,100,100);
}

Tomahawk.registerClass( TransformableObject, "TransformableObject" );
Tomahawk.extend( "TransformableObject", "Sprite" );

TransformableObject.prototype.minWidth 		= 20;
TransformableObject.prototype.minHeight 	= 20;
TransformableObject.prototype._body 		= null;
TransformableObject.prototype._corner1 		= null;
TransformableObject.prototype._corner2 		= null;
TransformableObject.prototype._corner3 		= null;
TransformableObject.prototype._corner4 		= null;
TransformableObject.prototype._active 		= false;
TransformableObject.prototype._activeObject = null;
TransformableObject.prototype.border 		= true;
TransformableObject.prototype.background 	= true;
TransformableObject.prototype.borderColor 	= "#000000";
TransformableObject.prototype.backgroundColor = "#ffffff";
TransformableObject.prototype.contrast 		= 0;
TransformableObject.prototype.brightness 	= 0;
TransformableObject.prototype._alpha 		= 1;

TransformableObject.prototype.allowDelete 		= true;
TransformableObject.prototype.allowResize 		= true;
TransformableObject.prototype.allowRotation 	= true;
TransformableObject.prototype.allowMove 		= true;

TransformableObject.prototype.init 				= function(x,y,width,height)
{
	this._border = new tomahawk_ns.Shape();
	this._back = new tomahawk_ns.Shape();
	
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	
	this._corner1	= new Corner('cross');
	this._corner2	= new Corner('rotation');
	this._corner3	= new Corner('resize');
	this._corner4	= new Corner('up');
	
	this._corner1.width = this._corner1.height = 20;
	this._corner2.width = this._corner2.height = 20;
	this._corner3.width = this._corner3.height = 20;
	this._corner4.width = this._corner4.height = 20;
	this._hasBorder = false;
	
	this.mouseEnabled = true;
	
	if( this._body == null )
	{
		var body = this._buildShape("white",width,height);
		body.width = this.width;
		body.height = this.height;
		this.setBody(body);
	}
	
	this.setActive(false);	
	this._back.mouseEnabled = this._border.mouseEnabled = false;
};

TransformableObject.prototype.getBody 			= function()
{
	return this._body;
};

TransformableObject.prototype.setActive 		= function(value)
{
	this._active = value;
	
	tomahawk_ns.Stage.getInstance().removeEventListener(tomahawk_ns.MouseEvent.MOUSE_UP, this,this._mouseHandler, true);
	tomahawk_ns.Stage.getInstance().removeEventListener(tomahawk_ns.MouseEvent.MOUSE_MOVE,this,this._mouseHandler, true);
	tomahawk_ns.Stage.getInstance().removeEventListener(tomahawk_ns.MouseEvent.DOUBLE_CLICK, this,this._mouseHandler, true);
	tomahawk_ns.Stage.getInstance().removeEventListener(tomahawk_ns.MouseEvent.CLICK, this,this._mouseHandler, true);
	
	this.removeEventListener(tomahawk_ns.MouseEvent.MOUSE_DOWN, this,this._mouseHandler, true);
	this.removeEventListener(tomahawk_ns.MouseEvent.ROLL_OVER, this,this._mouseHandler, true);
	
	tomahawk_ns.Stage.getInstance().addEventListener(tomahawk_ns.MouseEvent.CLICK, this,this._mouseHandler, true);
	this.addEventListener(tomahawk_ns.MouseEvent.MOUSE_DOWN, this,this._mouseHandler, true);
	
	if( this._active == true )
	{
		tomahawk_ns.Stage.getInstance().addEventListener(tomahawk_ns.MouseEvent.DOUBLE_CLICK, this,this._mouseHandler, true);
		tomahawk_ns.Stage.getInstance().addEventListener(tomahawk_ns.MouseEvent.MOUSE_UP, this,this._mouseHandler, true);
		tomahawk_ns.Stage.getInstance().addEventListener(tomahawk_ns.MouseEvent.MOUSE_MOVE, this,this._mouseHandler, true);
		this.addEventListener(tomahawk_ns.MouseEvent.ROLL_OVER, this,this._mouseHandler, true);
	}
	
	var type = (this._active == true) ? EditorEvent.SELECT_OBJECT : EditorEvent.UNSELECT_OBJECT;
	this.dispatchEvent( new EditorEvent(type,true,true) );
};

TransformableObject.prototype.setBody 			= function(body)
{
	if( this._body != null )
	{
		this._body._listeners = new Array();
		this.removeChild(this._body);
	}
	
	this._body = body;
	
	if( this._body != null )
	{
		//this._body.width = this.width;
		//this._body.height = this.height;
		this._body.mouseEnabled = true;
		this.addChild(this._body);
	}
};

TransformableObject.prototype._replaceCorners 	= function()
{
	if( this._body == null )
		return;
		
	var offset = this._corner1.width >> 1;
	if( this.name == "background" )
		offset = 0;
		
	var cornerWidth = this._corner1.width;
	
	if( this._active == true )
	{
		this.addChild(this._corner1);
		this.addChild(this._corner2);
		this.addChild(this._corner3);
		this.addChild(this._corner4);
		
		this._corner1.x = this._corner4.x = -offset;
		this._corner1.y = this._corner2.y = -offset;
		this._corner3.x = this._corner2.x = this._body.width - cornerWidth + offset;
		this._corner3.y = this._corner4.y = this._body.height - cornerWidth + offset;
	}
	else
	{
		this.removeChild(this._corner1);
		this.removeChild(this._corner2);
		this.removeChild(this._corner3);
		this.removeChild(this._corner4);
	}
	
	this._corner1.visible = this.allowDelete;
	this._corner2.visible = this.allowRotation;
	this._corner3.visible = this.allowResize;
};

TransformableObject.prototype._mouseHandler 	= function(event)
{	
	Mouse.setCursor(Mouse.DEFAULT);
	
	if( event.type == tomahawk_ns.MouseEvent.ROLL_OVER || event.type == tomahawk_ns.MouseEvent.MOUSE_MOVE)
	{
		if( event.target == this._corner1 )
			Mouse.setCursor(Mouse.KILL);
			
		if( event.target == this._corner2 )
			Mouse.setCursor(Mouse.ROTATE);
			
		if( event.target == this._corner3 )
			Mouse.setCursor(Mouse.RESIZE);
			
		if( event.target == this._body )
			Mouse.setCursor(Mouse.DRAG);
	}
	
	if( ( event.type == tomahawk_ns.MouseEvent.CLICK ) || ( event.type == tomahawk_ns.MouseEvent.MOUSE_DOWN ) )
	{
		this.setActive(event.target == this || event.target.isChildOf(this));
		this._replaceCorners();
		this._activeObject = null;
	}
	
	if( this._active == false )
		return;
	
	if( event.type == tomahawk_ns.MouseEvent.MOUSE_DOWN )
	{
		this._activeObject = event.target;
	}
		
	if( event.type == tomahawk_ns.MouseEvent.MOUSE_UP )
	{
		this._activeObject = null;
	}

	// transform
	if( this._activeObject == null )
		return;
		
	var coords = this.globalToLocal(event.stageX,event.stageY);
	var pt1 = null;
	var pt2 = null;
	
	if( this._activeObject == this._body && this.allowMove == true ) // drag
	{
		Mouse.setCursor(Mouse.DRAG);
		var pt = tomahawk_ns.Stage.getInstance().getMovement();
		this.x += pt.x;
		this.y += pt.y;
	}
	
	if( this._activeObject == this._corner1 && this.allowDelete == true )// kill the object
	{
		Mouse.setCursor(Mouse.KILL);
		if( this.parent != null )
		{
			this.parent.removeChild(this);
			return;
		}
	}
	
	if( this._activeObject == this._corner2 && this.allowRotation == true ) // rotate
	{
		Mouse.setCursor(Mouse.ROTATE);
		coords = this.localToGlobal(0, 0 );
		
		var dx = event.stageX - coords.x;
		var dy = event.stageY - coords.y;
		var angleRadian = Math.atan2( dy, dx );
		var angleDegree = Math.floor(angleRadian * ( 180 / Math.PI ));
		this.rotation = parseInt(angleDegree / 10) * 10;
	}
	
	if( this._activeObject == this._corner3 && this.allowResize == true ) // resize
	{
		Mouse.setCursor(Mouse.RESIZE);
		
		if( this.name == "background" )
		{
			var distanceX = coords.x - this._corner1.x + this._corner1.width * 0.5;
			var distanceY = coords.y - this._corner1.y + this._corner1.height * 0.5;
		}
		else
		{
			var distanceX = coords.x - this._corner1.x - this._corner1.width * 0.5;
			var distanceY = coords.y - this._corner1.y - this._corner1.height * 0.5;
		}
		this._body.width = ( distanceX < this.minWidth ) ? this.minWidth : distanceX;
		this._body.height = ( distanceY < this.minHeight ) ? this.minHeight : distanceY;
	}
	
	if( this._activeObject == this._corner4 && this.parent != null ) // childindex + 1
	{
		var index = this.parent.getChildIndex(this);
		this.parent.setChildIndex(this,index+1);
	}
	
	
	this._replaceCorners();
};

TransformableObject.prototype._buildShape 		= function(color,width,height)
{
	var shape = new Square();
	shape.width = width;
	shape.height = height;
	shape.color = color;
	shape.mouseEnabled = true;
	return shape;
};

TransformableObject.prototype._drawBorderAndBackground 		= function()
{
	this._border.clear();
	this._back.clear();
	
	if( this.border == true )
	{	
		this._border.width = this._body.width;
		this._border.height = this._body.height;
		this._border.x = this._body.x;
		this._border.y = this._body.y;
		
		
		this._border.clear();
		this._border.beginPath();
		this._border.strokeStyle(this.borderColor);
		this._border.lineWidth(2);
		this._border.moveTo( this._body.x , this._body.y );
		this._border.lineTo( this._body.x + this._body.width, this._body.y );
		this._border.lineTo( this._body.x + this._body.width, this._body.y + this._body.height );
		this._border.lineTo( this._body.x , this._body.y + this._body.height );
		this._border.lineTo( this._body.x , this._body.y );
		this._border.stroke();
		this._border.closePath();
	}
	
	if( this.background == true )
	{
		this._back.beginPath();
		this._back.fillStyle(this.backgroundColor);
		this._back.fillRect(this._body.x, this._body.y, this._body.width, this._body.height);
		this._back.fill();
		this._back.closePath();
	}
	
	this.addChildAt(this._back,0);
	this.addChild(this._border);
	this._replaceCorners();
};

TransformableObject.prototype.draw = function(context)
{	
	this._drawBorderAndBackground();
	tomahawk_ns.Sprite.prototype.draw.apply(this,[context]);
};

TransformableObject.prototype.unserialize = function(obj)
{
	

	this.border = obj.border;
	this.borderColor = obj.borderColor;
	this.background = obj.background;
	this.backgroundColor = obj.backgroundColor;
	this.allowRotation = obj.allowRotation;
	this.allowResize = obj.allowResize;
	this.allowMove = obj.allowMove;
	this.x = obj.x;
	this.y = obj.y;
	this.rotation = obj.rotation;
	this._body.width = obj.width;
	this._body.height = obj.height;
	this.minWidth = obj.minWidth;
	this.minHeight = obj.minHeight;
	this.name = obj.name;

	this.setContrast(obj.contrast);
	this.setBrightness(obj.brightness);
	this.setAlpha(obj.alpha);
	
	
	this._replaceCorners();
};

TransformableObject.prototype.serialize = function()
{
	var obj = new Object();
	obj.type = "TransformableObject";
	obj.border = this.border;
	obj.borderColor = this.borderColor;
	obj.background = this.background;
	obj.backgroundColor = this.backgroundColor;
	obj.allowRotation = this.allowRotation;
	obj.allowMove = this.allowMove;
	obj.allowResize = this.allowResize;
	obj.width = this._body.width;
	obj.height = this._body.height;
	obj.x = this.x;
	obj.y = this.y;
	obj.rotation = this.rotation;
	obj.minWidth = this.minWidth;
	obj.minHeight = this.minHeight;
	obj.hasBorder = this._hasBorder;
	obj.contrast = this.contrast;
	obj.brightness = this.brightness;
	obj.name = this.name;
	obj.alpha = this._alpha;
	
	return obj;
};

TransformableObject.prototype.setContrast = function(value)
{
	this.contrast =  parseInt(value);
};

TransformableObject.prototype.setBrightness = function(value)
{
	this.brightness =  parseInt(value);
};

TransformableObject.prototype.setAlpha = function(value)
{
	this._alpha = parseFloat(value);
};



